<?php

namespace IDock\Insurance\MACN\Response;

use IDock\Http\Response;
use IDock\Printer\NonCar\NonCarInquirySinglePrinter;
use IDock\Printer\NonCar\NonCarUnderwritingPrinter;
use IDock\Printer\PremiumUnderwritingPrinter;

class UnderwritingResponse extends AllMACNResponse
{

    public function resolve(): Response
    {
        $body = $this->getBody();
        $body = iconv('UTF-8','GBK',$body);
        if ($body) {
            libxml_disable_entity_loader();
            $body = simplexml_load_string($body);
            $body = $this->objectToArray($body);
            $errorCode = $body['head']['statusCode'] ?? '';
            $jqErr = $body['body']['baseInfo']['efcHandleText'] ?? '';
            $ciErr = $body['body']['baseInfo']['bizHandleText'] ?? '';
            if ($errorCode != 'SUCCESS') {
                $err = $body['head']['description'] ?? '未知错误或无错误提示 ';
                if(!empty($jqErr)){
                    $err .= $jqErr;
                }
                if(!empty($ciErr)){
                    $err .= $ciErr;
                }
                $this->setError($err);
                return  $this;
            }

            $this->setBody($body['body'] ?? '');
        }

        return $this;
    }

    public function printerInit(): Response
    {

        $premiumUnderwritingPrinter = new PremiumUnderwritingPrinter();
        $noCarPrinter = new NonCarUnderwritingPrinter();
        $body = $this->getBody();
        $baseInfo = $body['baseInfo'] ?? [];
        $premiumUnderwritingPrinter->inquiryNo = $baseInfo['askCalcNo'] ?? '';

        $premiumUnderwritingPrinter->biInquiryNo = $baseInfo['bizProposalNo'] ?? '';

        $premiumUnderwritingPrinter->ciInquiryNo = $baseInfo['efcProposalNo'] ?? '';
        $premiumUnderwritingPrinter->argc['preProposalTopNo'] = $baseInfo['preProposalTopNo'] ?? '';
        //非车信息
        if(!empty($baseInfo['acciProposalInfos'])){
            $noCarList = $baseInfo['acciProposalInfos']['acciProposalInfo'];
            if(!isset($noCarList[0])){
                $tmp = $noCarList;
                unset($noCarList);
                $noCarList[0] = $tmp;
            }
            foreach ($noCarList as $val){
                $single = new NonCarInquirySinglePrinter();
                $single->inquiryNo = $val['acciProposalNo'] ?? '';
                $single->productCode = $val['proCode'] ?? '';
                $single->planCode = $val['proCode'] ?? '';
                $noCarPrinter->nonCarSingleAppend($single);
            }
        }
        $biStats = $baseInfo['bizUnderWriteInd'] ?? '2';
        $biTip = $baseInfo['bizUnderWriteMsg'] ?? '';

        $ciStats = $baseInfo['efcUnderWriteInd'] ?? '2';
        $ciTip = $baseInfo['efcUnderWriteMsg'] ?? '';
        $premiumUnderwritingPrinter->argc['payUrl'] = $baseInfo['h5Url'] ?? '';
        $premiumUnderwritingPrinter->statusCode = 'U3';//默认人工核保
        $premiumUnderwritingPrinter->messageTip = $biTip .' '. $ciTip;
        if ($ciStats == '2' or $biStats == '2') {
            $premiumUnderwritingPrinter->statusCode = 'U2';//下发修改
            if ($biTip or $ciTip) {
                $premiumUnderwritingPrinter->messageTip = implode("\r\n", array_filter([$biTip . $ciTip]));
            } else {
                $premiumUnderwritingPrinter->messageTip = '转人工核保';
            }
        }

        if ($biStats == '4' or $ciStats == '4') {
            $premiumUnderwritingPrinter->statusCode = 'U4';//核保失败 拒保

        }

        if ( in_array($biStats,[3,5]) or in_array($ciStats,[3,5])) {
            $premiumUnderwritingPrinter->statusCode = 'U1';//核保通过
        }
        $premiumUnderwritingPrinter->noCarUnderwritingPrinter = $noCarPrinter;
        $this->printer = $premiumUnderwritingPrinter;
        return $this;
    }
}
